// pages/run/run.js
import WxValidate from "../../utils/WxValidate"
import utils from "../../utils/util"
const app = getApp()
Page({
  handleFooter() {
    wx.showModal({
      cancelColor: "cancelColor",
      title: "用户需知",
      content: "这里是隐私条款具体内容",
      showCancel: false,
    });
  },
  //选择快递商家
  businessChange(e) {
    this.setData({
      bussinessIndex: e.detail.value
    });
  },
  /* 跳转地址 */
  handleAddress() {
    wx.navigateTo({
      url: "../address/address",
    });
  },

  /*** 页面的初始数据 */
  data: {
    //multiArray是二维数组，picker多列选择器只支持二维数组
    multiArray: [
      ["手游", "端游"],
      ["王者荣耀", "和平精英", "阴阳师", "决战平安京", "第五人格", "其他手游"],
      ["手Q安卓", "手Q苹果", "微信安卓", "微信苹果", "手游其他"],
    ], //multiIndex数组，存放 “选中列” 的 “选中值” 对应的下标
    multiIndex: [0, 0, 0],
    form: {
      platform: '', //游戏平台 
      payMoney: 3, //支付金额 
      playDuration: '', //游戏时间或盘数 
      mark: '', //备注信息 
      gameID: '' //游戏ID 
    },
    baseMoney: 3 //基础跑腿费用
  },
  //多列选择器-值改变时触发
  pickerChange: function (e) {
    //console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      multiIndex: e.detail.value
    });
  },
  //多列选择器-列改变时触发
  pickerColumnChange: function (e) {
    //console.log(e) //console.log('修改的列为', e.detail.column, '，值为', e.detail.value);
    var data = {
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex,
    }; //存放 对应列 选中值 的下标 //e.detail.column 取值范围 0 1 2
    data.multiIndex[e.detail.column] = e.detail.value; //根据当前选择的列，更新后面列的值，当e.detail.column=3时，无需执行switch，
    switch (e.detail.column) {
      case 0: //当前滑动操作了第0列
        switch (data.multiIndex[0]) {
          case 0: //手游
            data.multiArray[1] = [
              "王者荣耀",
              "和平精英",
              "阴阳师",
              "决战平安京",
              "第五人格",
              "其他手游",
            ];
            data.multiArray[2] = [
              "手Q安卓",
              "手Q苹果",
              "微信安卓",
              "微信苹果",
              "手游 其他",
            ];
            break;
          case 1: //端游
            data.multiArray[1] = [
              "绝地求生",
              "英雄联盟",
              "穿越火线",
              "其他端游",
            ];
            data.multiArray[2] = ["端游大区"];
            break;
        } //后面两列默认 选择 为第0个元素
        data.multiIndex[1] = 0;
        data.multiIndex[2] = 0;
        break;
      case 1: //当前滑动操作了第1列
        switch (data.multiIndex[0]) {
          case 0: //手游
            switch (data.multiIndex[1]) {
              case 0: //王者荣耀
                data.multiArray[2] = [
                  "手Q安卓",
                  "手Q苹果",
                  "微信安卓",
                  "微信苹 果",
                  "手游其他",
                ];
                break;
              case 1: //和平精英
                data.multiArray[2] = ["手Q区", "微信区"];
                break;
              case 2: //阴阳师
                data.multiArray[2] = ["手Q区", "微信区"];
                break;
              case 3: //决战平安京
                data.multiArray[2] = ["手Q区", "微信区"];
                break;
              case 4: //第五人格
                data.multiArray[2] = ["手Q区", "微信区"];
                break;
              case 5: //其他手游
                data.multiArray[2] = ["手Q区", "微信区"];
                break;
            }
            break;
          case 1: //端游
            switch (data.multiIndex[1]) {
              case 0: //绝地求生
                data.multiArray[2] = ["端游大区"];
                break;
              case 1: //英雄联盟
                data.multiArray[2] = ["端游大区"];
                break;
              case 2: //穿越火线
                data.multiArray[2] = ["端游大区"];
                break;
              case 3: //其他端游
                data.multiArray[2] = ["端游大区"];
                break;
            }
            break;
        }
        data.multiIndex[2] = 0;
        break;
    }
    //console.log(data.multiIndex);
    this.setData(data);
  },
  //免责声明
  showStatment() {
    wx.showModal({
      title: "用户条款&隐私策略",
      content: "这里是隐私条款具体内容",
      showCancel: false,
    });
  },
  //计算金额
  countMoney(e) {
    //获取输入金额
    let money = e.detail.value;
    //判断是否为数字
    if (isNaN(money)) {
      wx.showToast({
        title: "请输入正确金额。",
        icon: "error"
      });
      return false;
    }
    if (money.trim() != "") {
      money = Number(money) + Number(this.data.baseMoney);
      this.setData({
        "form.payMoney": money
      });
    } else {
      this.setData({
        "form.payMoney": 3
      });
    }
  },
  //游戏事件或盘数、备注信息 游戏ID 
  timeID: 0, //定时器ID
  handleInput(e) {
    const {
      name
    } = e.currentTarget.dataset
    const {
      value
    } = e.detail
    clearTimeout(this.timeID);
    //清楚timeID对应的定时器，即取消相应函数的执行
    this.timeID = setTimeout(() => {
      switch (name) {
        case "playDuration":
          this.setData({
            "form.playDuration": value
          })
          break;
        case "mark":
          this.setData({
            "form.mark": value
          })
          break;
        case "gameID":
          this.setData({
            "form.gameID": value
          })
          break;
      }
    }, 500)
  },
  //保存表单 
  async submitForm() {
    //判断当前用户是否已授权登录 
    const r = await utils.cklogin(app.globalData.userInfo) 
    if (!r) {
      return false
    }
    const {
      multiArray,
      multiIndex
    } = this.data;
    const {
      playDuration,
      mark,
      gameID,
      payMoney
    } = this.data.form;
    //params为需要验证的数据对象 
    const params = {
      platform: `${multiArray[0][multiIndex[0]]}-${multiArray[1] [multiIndex[1]]}-${multiArray[2][multiIndex[2]]}`,
      playDuration,
      gameID,
      mark
    }
    //检测表单数据
    if (!this.WxValidate.checkForm(params)) {
      const error = this.WxValidate.errorList[0]
      utils.showValidateModal(error);
      return false
    }
    //声明发布数据时间变量，值为当前时间
    const publicTime = utils.formatTime(new Date());
    wx.request({
      url: `${app.globalData.server}/addOrder`,
      method: 'POST',
      data: {
        openid: app.globalData.userInfo.openid,
        orderType: '游戏陪玩',
        payMoney,
        publicTime,
        status: '待帮助',
        info: {
          platform: params.platform,
          playDuration,
          mark,
          gameID
        },
        userInfo: app.globalData.userInfo
      },
      success: (res) => {
        wx.hideLoading({})
        if (res.data == 'success') {
          wx.showToast({
            title: '提交成功',
            icon: 'success'
          })
          setTimeout(() => {
            wx.switchTab({
              url: '../order/order',
            })
          }, 1000);
        } else {
          wx.hideLoading({})
          wx.showToast({
            title: '发布失败',
            icon: 'error'
          })
        }
      },
      fail: (err) => {
        wx.hideLoading({})
        wx.showToast({
          title: '发布失败',
          icon: 'error'
        })
      }
    })
  },
  /*** 生命周期函数--监听页面加载 */
  onLoad(options) {
    this.initValidate();
  },
  initValidate() {
    // 验证字段的规则
    const rules = {
      platform: {
        required: true
      },
      playDuration: {
        required: true,
        maxlength: 20,
      },
      mark: {
        required: true,
        maxlength: 30
      },
      gameID: {
        required: true,
        maxlength: 20,
      }
    }
    // 验证字段的提示信息，若不传则调用默认的信息 
    const messages = {
      platform: {
        required: '请选择游戏平台',
      },
      playDuration: {
        required: '请输入游戏时间或盘数',
        maxlength: '游戏时间或盘数信息不能超过20个字符'
      },
      mark: {
        required: '请输入备注信息',
        maxlength: '备注信息最多输入30字'
      },
      gameID: {
        required: '请输入游戏ID',
        maxlength: '游戏ID最大不超过20字'
      }
    }
    // 创建实例对象 
    this.WxValidate = new WxValidate(rules, messages)
  },
  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});